//
//  MainViewController.swift
//  微博
//
//  Created by qiukai on 16/4/6.
//  Copyright © 2016年 仇恺. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //MARK: - 点击事件
    @objc private func didClickSendButton(sender: UIButton)
    {
        print("点了")
    }
    
    // MARK: - 懒加载
    /// 懒加载button
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    /// viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        addChildViewControllers()
        
        setUpComposedButton()
    }

}

extension MainViewController{
    /// 自定义按钮
    private func setUpComposedButton(){
//        添加按钮
        tabBar.addSubview(composedButton)
//        调整位置
        let count = childViewControllers.count
        let weight = tabBar.bounds.size.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, weight * 2, 0)

        composedButton.addTarget(self, action: "didClickSendButton:", forControlEvents: .TouchUpInside)
    }
    
    //MARK: - 添加控制器
    private func addChildViewControllers()
    {
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(ProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    /// 添加控制器
    ///
    /// - parameter vc:        要添加的控制器
    /// - parameter imageName: 底部按钮图片
    /// - parameter title:     顶部和底部文字
    private func addChildViewController(vc: UIViewController, imageName: String, title: String)
    {
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composedButton)
    }
}
